---
title: "recoding_unequal_congruence"
author: Marie Kübler
output: html_document
---
```{r, global options, warning=FALSE, include=FALSE}

# Loading R-libraries.

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, dplyr, haven, ggpubr, summarytools, kableExtra)

knitr::opts_chunk$set(warning=FALSE, message=FALSE, echo=TRUE)

```

# Preparation: Loading datasets

I use the candidate dataset and the cumulated voter dataset from the 2017 German Longitudinal Election Study (GLES). The metadata and an overview of all available data if can be found here: [Candidate survey 2017](https://doi.org/10.4232/1.13089) and [Candidate survey 2013](https://doi.org/10.4232/1.12043). The data is only available for academic research and teaching after the data depositors' written authorization. You can request access on the GLES website and order the data for a fee. The voter datasets can be downloaded freely via the GLES website after registration.[Pre- and Post-election Cross Section (Cumulation) 2017](https://doi.org/10.4232/1.13236)

```{r}
cand2017 <- read_sav("c17_ZA6814_v3-0-0.sav") %>% 
  select (gewaehlt, a1, c6, c2a, c2m, c9, c2b, c2h, c19e, c2k, c12, c2l, c2d, d6f, wei_mdb)
vot2017 <- read_sav("v17_cumulative_ZA6802_de_v3-0-1.sav") %>% 
  select (n11ba, vn60, v32a, n32a, v32i, n32k, vn59, v32c, n32c, v32d, n32d, v32e, n32e, v32g, n32g, vn61, v32b, n32b, v32j, n32l, v32f, n32f, vn140, vn149, vn157, vn162)

```

# Recoding

In this section, variables of candidates and citizens are recoded for subsequent use.

```{r}
## Being elected
cand2017$elect <- factor(cand2017$gewaehlt,
                             levels = c(0,1),
                             labels = c("not elected",
                                        "elected"))


# Party affiliation (in case of the citizens: party they voted for)

cand2017$party <- cand2017$a1
cand2017$party[cand2017$a1==7] <- 1
cand2017$party[cand2017$a1==4] <- 2
cand2017$party[cand2017$a1==6] <- 3
cand2017$party[cand2017$a1==5] <- 4
cand2017$party[cand2017$a1==2] <- 5
cand2017$party[cand2017$a1==3] <- 5
cand2017$party[cand2017$a1==322] <- 6

cand2017$party <- factor(cand2017$party,
                         levels= c(1:6),
                         labels= c("Left",
                                   "SPD", 
                                   "Greens", 
                                   "FDP", 
                                   "CDU-CSU", 
                                   "AfD"))


vot2017$party <- vot2017$n11ba
vot2017$party[vot2017$n11ba==7] <- 1
vot2017$party[vot2017$n11ba==4] <- 2
vot2017$party[vot2017$n11ba==6] <- 3
vot2017$party[vot2017$n11ba==5] <- 4
vot2017$party[vot2017$n11ba==1] <- 5
vot2017$party[vot2017$n11ba==322] <- 6
vot2017$party[vot2017$n11ba==801] <- 7

vot2017$party <- factor(vot2017$party,
                                levels= c(1:7),
                                labels= c("Left", 
                                          "SPD", 
                                          "Greens", 
                                          "FDP", 
                                          "CDU-CSU", 
                                          "AfD", 
                                          "other"))

```

Create a function for repeated labelling of factors with five levels: strongly agree to strongly disagree
```{r, warning=FALSE}

repeated_factor <- function(x) {
  factor (x, 
          levels=c(1,2,3,4,5),
          labels=c("strongly agree",
                   "agree",
                   "in part",
                   "disagree",
                   "strongly disagree"))
}


```

### Attitudes: immigration issue
```{r}
cand2017$immig <- factor(cand2017$c6,
                                      levels = c(1:11))
vot2017$immig <- factor(vot2017$vn60,
                                     levels = c(1:11))

```

### Attitudes: assimilation immigrants
```{r}

# vot2017: combine two variables 
vot2017$v32a_1 <- vot2017$v32a
vot2017$n32a_1 <- vot2017$n32a
vot2017$v32a_1[is.na(vot2017$v32a_1)] <- 0
vot2017$n32a_1[is.na(vot2017$n32a_1)] <- 0
vot2017$assim <- vot2017$v32a_1 + vot2017$n32a_1
vot2017$assim [vot2017$assim == 0] <- NA

cand2017$assim_fac <- repeated_factor(cand2017$c2a)

vot2017$assim_fac <- repeated_factor(vot2017$assim)

cand2017$assim <- as.numeric(cand2017$assim_fac)

vot2017$assim <- as.numeric(vot2017$assim_fac)

```

### Attitudes: limit refugees
```{r}

vot2017$v32i_1 <- vot2017$v32i
vot2017$n32k_1 <- vot2017$n32k
vot2017$v32i_1[is.na(vot2017$v32i_1)] <- 0
vot2017$n32k_1[is.na(vot2017$n32k_1)] <- 0
vot2017$lim_ref <- vot2017$v32i_1 + vot2017$n32k_1
vot2017$lim_ref [vot2017$lim_ref== 0] <- NA


cand2017$limit_fac <- repeated_factor(cand2017$c2m)

vot2017$limit_fac <- repeated_factor(vot2017$lim_ref)

cand2017$limit <- as.numeric(cand2017$limit_fac)

vot2017$limit <- as.numeric(vot2017$limit_fac)



```

### Attitudes: welfare state
```{r, warning=FALSE}

cand2017$welf <- factor(cand2017$c9,
                                        levels = c(1:11))
vot2017$welf <- factor(vot2017$vn59,
                                        levels = c(1:11))
```

### Attitudes: relations politics - economy
```{r}

vot2017$v32c_1 <- vot2017$v32c
vot2017$n32c_1 <- vot2017$n32c
vot2017$v32c_1[is.na(vot2017$v32c_1)] <- 0
vot2017$n32c_1[is.na(vot2017$n32c_1)] <- 0
vot2017$polec <- vot2017$v32c_1 + vot2017$n32c_1
vot2017$polec [vot2017$polec == 0] <- NA

cand2017$pol_econ_fac <- repeated_factor(cand2017$c2b)

vot2017$pol_econ_fac <- repeated_factor(vot2017$polec)

cand2017$pol_econ <- as.numeric(cand2017$pol_econ_fac)

vot2017$pol_econ <- as.numeric(vot2017$pol_econ_fac)

```

### Attitudes: inequality
```{r}

vot2017$v32d_1 <- vot2017$v32d
vot2017$n32d_1 <- vot2017$n32d
vot2017$v32d_1[is.na(vot2017$v32d_1)] <- 0
vot2017$n32d_1[is.na(vot2017$n32d_1)] <- 0
vot2017$inequ <- vot2017$v32d_1 + vot2017$n32d_1
vot2017$inequ [vot2017$inequ == 0] <- NA

cand2017$inequ_fac <- repeated_factor(cand2017$c2h)

vot2017$inequ_fac <- repeated_factor(vot2017$inequ)

cand2017$inequ <- as.numeric(cand2017$inequ_fac)

vot2017$inequ <- as.numeric(vot2017$inequ_fac)

```

### Attitudes: EU redistribution
```{r}

vot2017$v32e_1 <- vot2017$v32e
vot2017$n32e_1 <- vot2017$n32e
vot2017$v32e_1[is.na(vot2017$v32e_1)] <- 0
vot2017$n32e_1[is.na(vot2017$n32e_1)] <- 0
vot2017$EU_re <- vot2017$v32e_1 + vot2017$n32e_1
vot2017$EU_re [vot2017$EU_re == 0] <- NA

cand2017$EU_fac <- repeated_factor(cand2017$c19e)

vot2017$EU_fac <- repeated_factor(vot2017$EU_re)

cand2017$EU <- as.numeric(cand2017$EU_fac)

vot2017$EU <- as.numeric(vot2017$EU_fac)

```

### Attitudes: nuclear power
```{r}

vot2017$v32g_1 <- vot2017$v32g
vot2017$n32g_1 <- vot2017$n32g
vot2017$v32g_1[is.na(vot2017$v32g_1)] <- 0
vot2017$n32g_1[is.na(vot2017$n32g_1)] <- 0
vot2017$nuc <- vot2017$v32g_1 + vot2017$n32g_1
vot2017$nuc [vot2017$nuc == 0] <- NA

cand2017$nuc_fac <- repeated_factor(cand2017$c2k)

vot2017$nuc_fac <- repeated_factor(vot2017$nuc)

cand2017$nuc <- as.numeric(cand2017$nuc_fac)

vot2017$nuc <- as.numeric(vot2017$nuc_fac)

```

### Attitudes: climate protection
```{r, warning=FALSE}

cand2017$clim <- factor (cand2017$c12,
                                         levels = c(1:11))
vot2017$clim <- factor (vot2017$vn61,
                                        levels = c(1:11))

```

### Attitudes: gender quota
```{r}

vot2017$v32b_1 <- vot2017$v32b
vot2017$n32b_1 <- vot2017$n32b
vot2017$v32b_1[is.na(vot2017$v32b_1)] <- 0
vot2017$n32b_1[is.na(vot2017$n32b_1)] <- 0
vot2017$quota <- vot2017$v32b_1 + vot2017$n32b_1
vot2017$quota [vot2017$quota == 0] <- NA

cand2017$quota_fac <- repeated_factor(cand2017$c2l)

vot2017$quota_fac <- repeated_factor(vot2017$quota)

cand2017$quota <- as.numeric(cand2017$quota_fac)

vot2017$quota <- as.numeric(vot2017$quota_fac)

```

### Attitudes: same-sex-marriage
```{r}

vot2017$v32j_1 <- vot2017$v32j
vot2017$n32l_1 <- vot2017$n32l
vot2017$v32j_1[is.na(vot2017$v32j_1)] <- 0
vot2017$n32l_1[is.na(vot2017$n32l_1)] <- 0
vot2017$ssm <- vot2017$v32j_1 + vot2017$n32l_1
vot2017$ssm [vot2017$ssm== 0] <- NA


cand2017$ssmar_fac <- factor (cand2017$c2d,
                                  levels=c(5,4,3,2,1),
                             labels=c("strongly agree",
                   "agree",
                   "in part",
                   "disagree",
                   "strongly disagree"))

vot2017$ssmar_fac <- repeated_factor(vot2017$ssm)

cand2017$ssmar <- as.numeric(cand2017$ssmar_fac)

vot2017$ssmar <- as.numeric(vot2017$ssmar_fac)

```


### Attitudes: referendum
```{r}

vot2017$v32f_1 <- vot2017$v32f
vot2017$n32f_1 <- vot2017$n32f
vot2017$v32f_1[is.na(vot2017$v32f_1)] <- 0
vot2017$n32f_1[is.na(vot2017$n32f_1)] <- 0
vot2017$ref <- vot2017$v32f_1 + vot2017$n32f_1
vot2017$ref [vot2017$ref == 0] <- NA

cand2017$ref_fac <- repeated_factor(cand2017$d6f)

vot2017$ref_fac <- repeated_factor(vot2017$ref)

cand2017$ref <- as.numeric(cand2017$ref_fac)

vot2017$ref <- as.numeric(vot2017$ref_fac)

```


```{r}
# combining current employment, former employment, partner's employment and partner's former employment in one new variable

vot2017$occup <- if_else(!is.na(vot2017$vn140), vot2017$vn140, vot2017$vn149)
vot2017$occup <- if_else(!is.na(vot2017$occup), vot2017$occup, vot2017$vn157)
vot2017$occup <- if_else(!is.na(vot2017$occup), vot2017$occup, vot2017$vn162)

vot2017$occup[vot2017$occup>=11 & vot2017$occup<21] <- 1
vot2017$occup[vot2017$occup>=21 & vot2017$occup<24] <- 2
vot2017$occup[vot2017$occup>=24 & vot2017$occup<26] <- 3
vot2017$occup[vot2017$occup>=31 & vot2017$occup<40] <- 4
vot2017$occup[vot2017$occup>=41 & vot2017$occup<51] <- 5
vot2017$occup[vot2017$occup>=51 & vot2017$occup<61] <- 5
vot2017$occup[vot2017$occup>=61 & vot2017$occup<70] <- NA
vot2017$occup[vot2017$occup== -97] <- NA
vot2017$occup[vot2017$occup== -99] <- NA

vot2017$occup <- factor(vot2017$occup,
                            levels=c(1,2,3,4,5),
                            labels=c("worker",
                                     "lower employee",
                                     "higher employee",
                                     "civil servant",
                                     "business owner"))
```



## dataset MPs only:
```{r}

MP2017 <- filter(cand2017, elect == "elected")

```

# safe datasets

```{r}
save(list = c("cand2017", "MP2017", "vot2017"), file = "unequal_congruence.Rdata")
```


